/*
 * Copyright 2007 Yegor Jbanov
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 *   
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.spicesoft.clientobjects.samples.list;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import com.spicesoft.clientobjects.BeanEnhancer;
import com.spicesoft.clientobjects.BeanRegistry;

public class ListSample {

  private CountryListState bean;

  private CountryListForm applicationWindow;

  /**
   * This is where the programs starts from
   */
  public static void main(String[] args) {
    new ListSample().start();
  }

  /**
   * Initialises the application
   */
  public void start() {
    // Register POJOs with Client-Objects
    BeanRegistry.registerType(CountryListState.class);

    // Create POJO
    CountryListState pojo = new CountryListState();
    pojo.getCountries().add("Canada");
    pojo.getCountries().add("United Kingdom");
    pojo.getCountries().add("France");

    // Convert POJO to JavaBean (i.e. add property change support)
    // This JavaBean will represent the UI state object
    // (in other words, cast a Client-Objects spell on the POJO)
    bean = BeanEnhancer.addPropertyChangeSupport(pojo);

    // Create application frame and pass the bean to operate on
    applicationWindow = new CountryListForm(bean);

    // Add action listeners
    applicationWindow.addDeleteListener(this.deleteListener);
    applicationWindow.addAddListener(this.addListener);
    applicationWindow.addUpdateListener(this.updateListener);
    applicationWindow.addCloseListener(this.closeListener);

    // Configure and show the application window
    applicationWindow.pack();
    applicationWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    applicationWindow.setLocationRelativeTo(null);
    applicationWindow.setVisible(true);
  }

  /**
   * Adds a country (from the text box) to the list, if it's not already in the
   * list. Note that we are not accessing the GUI elements of the form to read
   * and manipulate the values. All values are already in the bean, because it
   * is bound to the GUI elements through JGoodies binding.
   */
  private ActionListener addListener = new ActionListener() {
    public void actionPerformed(ActionEvent event) {
      if (bean.getCountries().contains(bean.getCountryName())) {
        JOptionPane.showMessageDialog(applicationWindow, "Country '"
            + bean.getCountryName() + "' is already listed");
      } else {
        // Add it
        bean.getCountries().add(bean.getCountryName());
        
        // Select it
        bean.setSelectedCountry(bean.getCountryName());
      }
    }
  };

  /**
   * Updates a country's name.
   */
  private ActionListener updateListener = new ActionListener() {
    public void actionPerformed(ActionEvent event) {
      if (bean.getCountries().contains(bean.getCountryName())) {
        JOptionPane.showMessageDialog(applicationWindow, "Country '"
            + bean.getCountryName() + "' is already listed");
      } else {
        bean.getCountries().set(bean.getSelectedIndex(), bean.getCountryName());
      }
    }
  };

  /**
   * Remove a country's name from the list.
   */
  private ActionListener deleteListener = new ActionListener() {
    public void actionPerformed(ActionEvent event) {
      if (bean.getSelectedIndex() < 0) {
        JOptionPane.showMessageDialog(applicationWindow,
            "Please, select a country first.");
      } else {
        bean.getCountries().remove(bean.getSelectedIndex());
      }
    }
  };

  /**
   * Closes the window (and exits the application, thanks to EXIT_ON_CLOSE, see
   * code above)
   */
  private ActionListener closeListener = new ActionListener() {
    public void actionPerformed(ActionEvent event) {
      applicationWindow.dispose();
    }
  };

}
